01 / 18

What are some real-world applications of Stacks? (Undo/Redo, Browser History)

Stack Applications

  1. 1

    Undo functionality: recent actions are pushed and popped in reverse order.

  2. 2

    Redo functionality: a second stack can track actions that were undone.

  3. 3

    Browser history can use stacks to model backward and forward navigation.

  4. 4

    Function calls and recursion use the runtime call stack.

  5. 5

    Expression parsing and evaluation use stacks.

  6. 6

    Depth-First Search can be implemented using an explicit stack.

  7. 7

    Backtracking algorithms use stacks to represent previous states.

Difficulty: 4/10
Topics: Stack Applications, Undo-Redo Mechanisms, State Management

Scenario Questions

0-2 years experience
  1. 1

    We're building a simple text editor and want to add an 'Undo' feature. If a user types 'hello', then 'world', and hits undo, how would you use a stack to revert the last change? What exactly gets pushed onto the stack?

  2. 2

    Imagine you're implementing a browser's back button using a stack. When the user visits Page A, then Page B, and clicks 'Back', how do you track where they came from, and what happens to Page B once they go back?

2-5 years experience
  1. 1

    We implemented an undo/redo feature using two stacks, but users are reporting that after they undo three actions and then perform a new action, the 'Redo' button behaves unpredictably or restores stale states. What's going wrong in our stack coordination, and how do we fix it?

  2. 2

    In our collaborative design tool, saving the entire canvas state onto an undo stack on every mouse drag is causing severe memory lag and occasional crashes. How would you refactor this stack-based undo system to be more memory-efficient without losing the ability to revert changes?

5-8 years experience
  1. 1

    We need to design a multi-level undo/redo system for a complex web-based IDE that supports file edits, folder creations, and git operations. How would you design the command history stack to handle heterogeneous actions, and how do you enforce a strict memory limit on these stacks?

  2. 2

    Our mobile app uses a stack-based navigation history. When users deep-link into a nested page, hit the back button, and expect to navigate through a logical hierarchy rather than their actual history, the stack model breaks down. How would you design a hybrid navigation system that reconciles historical back-clicks with hierarchical back-clicks?

8+ years experience
  1. 1

    We are migrating a massive legacy desktop application to a collaborative, cloud-based SaaS platform. The desktop app relied on a local, in-memory command stack for undo/redo. How do we architect a distributed undo/redo system that handles concurrent edits from multiple users while maintaining a consistent, intuitive undo stack for each individual user?

  2. 2

    Our enterprise platform's state-management architecture is built around a centralized undo/redo stack. As teams add new micro-frontends, the global stack is becoming a bottleneck, leading to state pollution and memory leaks. How would you federate or redesign this history-tracking architecture to allow independent teams to manage their own feature history while maintaining a cohesive user experience?

Follow-up Questions

  • How would you handle undoing an action that failed halfway through execution?
  • What are the trade-offs of storing full state snapshots versus delta changes on your history stack?
  • How do you enforce a maximum size on your stack to prevent memory leaks in a long-running session?